package com.tianbing.adlottery.controller.storeadmin;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.fengniu.ejb.business.ICouponBusiness;
import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ICategoryDao;
import com.fengniu.ejb.dao.IContentDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.ISystemDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.CouponItem;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.Site2Content;
import com.fengniu.ejb.entity.User;
import com.fengniu.ejb.entity.User2Site;
import com.tianbing.adlottery.controller.user.AdminMySaleController;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UserSession;
import com.tianbing.adlottery.util.UtilDataGroup;

/**
 * MultiActionController for admin my store.
 * 
 * @author Conan Chen
 * @since 2007.12.15
 */
public class AdminMyStoreController extends MultiActionController {

	Logger logger = Logger.getLogger(AdminMySaleController.class.getName());

	private String myStoreProductListView;

	private String myStoreCouponListView;

	private String myStoreCouponItemListView;

	private String approveMyStoreCouponSuccView;

	private String representMyStoreCouponItemSuccView;

	private String addProduct2MyStoreSuccView;

	private String deleteProductFromMyStoreSuccView;

	private String userSimpleProfileView;

	private String requestMyStoreServantView;

	private IUserBusiness userBusiness;

	private ISiteBusiness siteBusiness;

	private ICouponBusiness couponBusiness;

	private ISiteDao siteDao;

	private IUserDao userDao;

	private IContentDao contentDao;

	private ICategoryDao categoryDao;

	private SkinUtils skinUtils;

	private ISystemDao systemDao;

	public ModelAndView listMyStoreProduct(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(myStoreProductListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long ssid = auth.getManagedStoreSysId();
		// get site2content for this site
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");
		Integer[] relationIds = { IConstants.RelationID.S2C_IS_SELLER_OF.getId() };
		List site2ContentList = siteDao.getListSite2ContentBy(ssid, relationIds, -1, null, null, null, null, pgno,
				pgsize, odby);
		Long totalCount = siteDao.getCountSite2ContentBy(ssid, relationIds, -1, null, null, null, null);
		List site2ContentGroupList = UtilDataGroup.makeGroups(site2ContentList, 0, 6);

		model.put("site2ContentGroupList", site2ContentGroupList);
		model = skinUtils.preparePageFooter(request, site2ContentList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView listMyStoreCoupon(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(myStoreCouponListView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);

		long ssid = auth.getManagedStoreSysId();
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		Site siteDetail = siteDao.findSiteBySysId(ssid);

		Map<String, Object> s2cMap = couponBusiness.getCoupons(ssid, pgno, pgsize);

		List<Site2Content> site2ContentList = (List) s2cMap.get("site2ContentList");
		Long totalCount = (Long) s2cMap.get("totalCount");
		List site2ContentGroupList = UtilDataGroup.makeGroups(site2ContentList, 0, 4);

		model.put("siteDetail", siteDetail);
		model.put("site2ContentGroupList", site2ContentGroupList);

		model = skinUtils.preparePageFooter(request, site2ContentList, totalCount, pgno, pgsize, model);

		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView listMyStoreCouponItem(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		ModelAndView mv = new ModelAndView(myStoreCouponItemListView);
		Map<String, Object> model = mv.getModel();

		long csid = ServletRequestUtils.getLongParameter(request, "csid", -1); // coupon
		// sysId
		long ssid = auth.getManagedStoreSysId();
		long issueid = ServletRequestUtils.getLongParameter(request, "issueid", -1);
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);
		if (csid > 0) {
			Site2Content store2Coupon = couponBusiness.findCoupon(csid, ssid);
			model.put("store2Coupon", store2Coupon);
		}
		// ------------
		Map storeCouponItems = couponBusiness.getCouponItems(-1, ssid, csid, issueid, pgno, pgsize);
		List<CouponItem> couponItemList = (List<CouponItem>) storeCouponItems.get("couponItemList");
		Long totalCount = (Long) storeCouponItems.get("totalCount");

		model.put("couponItemList", couponItemList);
		model.put("totalCount", totalCount);

		model = skinUtils.preparePageFooter(request, couponItemList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView representMyStoreCouponItem(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long csid = ServletRequestUtils.getLongParameter(request, "csid", -1); // coupon
		// sysId

		ModelAndView mv = new ModelAndView(representMyStoreCouponItemSuccView + "?csid=" + csid);
		Map<String, Object> model = mv.getModel();

		long ssid = auth.getManagedStoreSysId();
		long issueid = ServletRequestUtils.getLongParameter(request, "issueId", -1); // coupon item issue id
		long usid = ServletRequestUtils.getLongParameter(request, "usid", -1); // customer
		// userId
		String couponItemId = ServletRequestUtils.getStringParameter(request, "couponItemId", ""); // customer userId
		String shortDesc = ServletRequestUtils.getStringParameter(request, "shortDesc", ""); // customer userId

		if (csid > 0 && usid > 0) {
			User user = userDao.findUserBySysId(usid);
			if (user != null) {
				Site2Content store2Coupon = couponBusiness.findCoupon(csid, ssid);
				model.put("store2Coupon", store2Coupon);
				if (store2Coupon != null) {
					couponBusiness.representCouponItem(ssid, csid, usid, couponItemId, issueid, shortDesc);
				}
			}
		}

		return mv;
	}

	public ModelAndView getUserSimpleProfile(HttpServletRequest request, HttpServletResponse response) throws Exception {

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		String id = ServletRequestUtils.getStringParameter(request, "id", null); // user
		// id
		// e.g.
		// telphone,fax,email,sysId

		ModelAndView mv = new ModelAndView(userSimpleProfileView);
		Map<String, Object> model = mv.getModel();

		User user = userDao.findUserByArbitraryId(id);
		model.put("user", user);
		return mv;
	}

	public void setRepresentMyStoreCouponItemSuccView(String representMyStoreCouponItemSuccView) {
		this.representMyStoreCouponItemSuccView = representMyStoreCouponItemSuccView;
	}

	public void setCouponBusiness(ICouponBusiness couponBusiness) {
		this.couponBusiness = couponBusiness;
	}

	public void setApproveMyStoreCouponSuccView(String approveMyStoreCouponSuccView) {
		this.approveMyStoreCouponSuccView = approveMyStoreCouponSuccView;
	}

	public ModelAndView addProduct2MyStore(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// process wordtext parameter
		long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		if (csid > 0) {
			siteBusiness.addStoreSellProduct(csid, auth.getManagedStoreSysId());
		}
		return new ModelAndView(addProduct2MyStoreSuccView);
	}

	public ModelAndView approveMyStoreCoupon(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// process wordtext parameter
		ModelAndView mv = new ModelAndView(approveMyStoreCouponSuccView);
		return mv;
	}

	public ModelAndView requestMyStoreServant(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView(requestMyStoreServantView);
		Map<String, Object> model = mv.getModel();
		// process wordtext parameter
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		String userId = ServletRequestUtils.getStringParameter(request, "userId", null);
		if (userId != null) {
			userBusiness.requestStoreServant(auth.getManagedStoreSysId(), userId);
		}

		User2Site storeServant = userBusiness.findStoreServant(auth.getManagedStoreSysId());
		model.put("storeServant", storeServant);
		model.put("userId", userId);

		return mv;
	}

	public ModelAndView deleteProductFromMyStore(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// process wordtext parameter
		long s2csid = ServletRequestUtils.getLongParameter(request, "s2csid", -1);
		siteDao.deleteSite2Content(s2csid);
		return new ModelAndView(deleteProductFromMyStoreSuccView);
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setMyStoreProductListView(String homeView) {
		this.myStoreProductListView = homeView;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setSystemDao(ISystemDao systemDao) {
		this.systemDao = systemDao;
	}

	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setAddProduct2MyStoreSuccView(String addProduct2MyStoreSuccView) {
		this.addProduct2MyStoreSuccView = addProduct2MyStoreSuccView;
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}

	public void setDeleteProductFromMyStoreSuccView(String deleteProductFromMyStoreSuccView) {
		this.deleteProductFromMyStoreSuccView = deleteProductFromMyStoreSuccView;
	}

	public void setRequestMyStoreServantView(String requestMyStoreServantView) {
		this.requestMyStoreServantView = requestMyStoreServantView;
	}

	public void setMyStoreCouponListView(String myStoreCouponListView) {
		this.myStoreCouponListView = myStoreCouponListView;
	}

	public void setMyStoreCouponItemListView(String myStoreCouponItemListView) {
		this.myStoreCouponItemListView = myStoreCouponItemListView;
	}

	public void setUserSimpleProfileView(String userSimpleProfileView) {
		this.userSimpleProfileView = userSimpleProfileView;
	}

}
